package com.shujia.flink.window;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo03CountWindow {
    public static void main(String[] args) throws Exception {
        // 计数窗口：滚动、滑动
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> ds = env.socketTextStream("master", 8888);

        ds.map(word-> Tuple2.of(word,1), Types.TUPLE(Types.STRING,Types.INT))
                .keyBy(t2->t2.f0)
//                .countWindow(5) // 每同一个key的5条数据会统计一次
                .countWindow(10,5) // 每隔同一个key的5条数据统计最近10条数据
                .sum(1)
                .print();

        env.execute();


    }
}
